<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="年龄年份" prop="ageYear">
        <el-input v-model="dataForm.ageYear" placeholder="年龄年份"></el-input>
      </el-form-item>
      <el-form-item label="年龄月份" prop="ageMonth">
        <el-input v-model="dataForm.ageMonth" placeholder="年龄月份"></el-input>
      </el-form-item>
      <el-form-item label="BMIP50th" prop="bmi50">
        <el-input v-model="dataForm.bmi50" placeholder="BMIP50th"></el-input>
      </el-form-item>
      <el-form-item label="BMIP85th" prop="bmi85">
        <el-input v-model="dataForm.bmi85" placeholder="BMIP85th"></el-input>
      </el-form-item>
      <el-form-item label="BMIP97th" prop="bmi97">
        <el-input v-model="dataForm.bmi97" placeholder="BMIP97th"></el-input>
      </el-form-item>
      <el-form-item label="BMIP95th" prop="bmi95">
        <el-input v-model="dataForm.bmi95" placeholder="BMIP95th"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="dataForm.gender" placeholder="请选择">
          <el-option label="男" :value="0"></el-option>
          <el-option label="女" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <!--    <el-form-item label="创建时间" prop="createTime">-->
      <!--      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="修改时间" prop="updateTime">-->
      <!--      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="	创建用户id" prop="createUser">-->
      <!--      <el-input v-model="dataForm.createUser" placeholder="	创建用户id"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="修改用户id" prop="updateUser">-->
      <!--      <el-input v-model="dataForm.updateUser" placeholder="修改用户id"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="单位id" prop="unitId">-->
      <!--      <el-input v-model="dataForm.unitId" placeholder="单位id"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="租户id" prop="tenantId">-->
      <!--      <el-input v-model="dataForm.tenantId" placeholder="租户id"></el-input>-->
      <!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    getChildrenBmiObj,
    addChildrenBmiObj,
    putChildrenBmiObj
  } from '@/api/pvm/childrenbmi'

  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          ageYear: '',
          ageMonth: '',
          bmi50: '',
          bmi85: '',
          bmi97: '',
          bmi95: '',
          gender: '',
          createTime: '',
          updateTime: '',
          createUser: '',
          updateUser: '',
          unitId: '',
          tenantId: ''
        },
        dataRule: {
          ageYear: [{
            required: true,
            message: '年龄年份不能为空',
            trigger: 'blur'
          }],
          ageMonth: [{
            required: true,
            message: '年龄月份不能为空',
            trigger: 'blur'
          }],
          bmi50: [{
            required: true,
            message: 'BMIP50th不能为空',
            trigger: 'blur'
          }],
          bmi85: [{
            required: true,
            message: 'BMIP85th不能为空',
            trigger: 'blur'
          }],
          bmi97: [{
            required: true,
            message: 'BMIP97th不能为空',
            trigger: 'blur'
          }],
          bmi95: [{
            required: true,
            message: 'BMIP95th不能为空',
            trigger: 'blur'
          }],
          gender: [{
            required: true,
            message: '性别(1男,0女)不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建时间不能为空',
            trigger: 'blur'
          }],
          updateTime: [{
            required: true,
            message: '修改时间不能为空',
            trigger: 'blur'
          }],
          createUser: [{
            required: true,
            message: '	创建用户id不能为空',
            trigger: 'blur'
          }],
          updateUser: [{
            required: true,
            message: '修改用户id不能为空',
            trigger: 'blur'
          }],
          unitId: [{
            required: true,
            message: '单位id不能为空',
            trigger: 'blur'
          }],
          tenantId: [{
            required: true,
            message: '租户id不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getChildrenBmiObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putChildrenBmiObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            } else {
              addChildrenBmiObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
              })
            }
          }
        })
      }
    }
  }
</script>
